package com.cuotigou.controller;

import com.cuotigou.config.ConfigUtils;
import com.cuotigou.mapper.SOfferRewardMapper;
import com.cuotigou.mapper.SWrongTopicMapper;
import com.cuotigou.pojo.SOfferReward;
import com.cuotigou.pojo.SWrongTopic;
import com.cuotigou.service.WrongTopicService;
import com.cuotigou.util.*;
import com.sun.net.httpserver.HttpServer;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/31.
 */
@Controller
@RequestMapping("/wrongTopic")
public class WrongTopicController {

    @Autowired
    private WrongTopicService wrongTopicService;
    @Autowired
    private SWrongTopicMapper sWrongTopicMapper;
    @Autowired
    private SOfferRewardMapper sOfferRewardMapper;


    /**
     * 主界面
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult index(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.index(xmlBean);
    }

    //错题查询
    @RequestMapping(value = "/subjectDetail",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult subjectDetail(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        if("1".equals(xmlBean.getTypeNew())) {
            return wrongTopicService.subjectWrongTopicDetails(xmlBean);
        }
        else if ("2".equals(xmlBean.getTypeNew())){
            return wrongTopicService.wrongTopicScreen(xmlBean);
        }else {
            return ObjectResult.build("0","请求参数错误");
        }
    }

    /**
     * 错题录入
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/upload")
    @ResponseBody
    public ObjectResult upload(XMLBean xmlBean, @RequestParam("stemFiles") MultipartFile[] stemFiles
            ,@RequestParam("answerFiles") MultipartFile[] answerFiles, @RequestParam("originalFiles") MultipartFile[] originalFiles){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }

        return wrongTopicService.wrongTopicupload(xmlBean,stemFiles,answerFiles,originalFiles);
    }

    /**
     * 错题删除
     * @param xmlBean
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public ObjectResult delete(XMLBean xmlBean,HttpServletRequest request){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }

        return wrongTopicService.wrongTopicupDelete(xmlBean);
    }

    /**
     * 彻底删除
     * @param xmlBean
     * @param request
     * @return
     */
    @RequestMapping(value = "/realDelete")
    @ResponseBody
    public ObjectResult realDelete(XMLBean xmlBean,HttpServletRequest request){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }

        return wrongTopicService.wrongTopicupRealDelete(xmlBean);
    }
    /**
     * 修改错题
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public ObjectResult update(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.wrongTopicupUpdate(xmlBean);
    }

    /**
     * 编辑错题
     * @param xmlBean
     * @param request
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public ObjectResult edit(XMLBean xmlBean,HttpServletRequest request,@RequestParam("stemFiles") MultipartFile[] stemFiles,
                             @RequestParam("answerFiles")MultipartFile[] answerFiles,@RequestParam("originalFiles") MultipartFile[] originalFiles){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.wrongTopicupEdit(xmlBean,stemFiles,answerFiles,originalFiles);
    }

    /**
     * 加入错题本
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public ObjectResult add(XMLBean xmlBean,HttpServletResponse response){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        return wrongTopicService.wrongTopicAdd(xmlBean);
    }


    /**
     * 用户的科目
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/subject",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult subject(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.subject(xmlBean);
    }

//    /**
//     * 热门题目
//     * @param xmlBean
//     * @return
//     */
//    @RequestMapping(value = "/hot",method = RequestMethod.POST, produces = "application/json")
//    @ResponseBody
//    public ObjectResult hot(XMLBean xmlBean){
////        if(StringUtil.isEmpty(xmlBean.getToken())){
////            return ObjectResult.build("0","未登录");
////        }
//        return wrongTopicService.wrongTopicHot(xmlBean);
//    }

    /**
     * 题库复习
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/review",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult review(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.review(xmlBean);
    }

    /**
     * 错题筛选
     * @param xmlBean
     * @return
     */
//    @RequestMapping(value = "/screen",method = RequestMethod.POST, produces = "application/json")
//    @ResponseBody
//    public ObjectResult screen(XMLBean xmlBean){
//        if(StringUtil.isEmpty(xmlBean.getToken())){
//            return ObjectResult.build("0","未登录");
//        }
//        return wrongTopicService.wrongTopicScreen(xmlBean);
//    }

    /**
     * 错题详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult detail(XMLBean xmlBean,HttpServletResponse response){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
//
        response.setHeader("Access-Control-Allow-Origin", "*");
        ObjectResult objectResult = wrongTopicService.wrongTopicDetails(xmlBean);
        if("0".equals(objectResult.getCode())){
            return objectResult;
        }else {
            SWrongTopic sWrongTopic = (SWrongTopic)objectResult.getData();
            sWrongTopic.setAnswer(StringEscapeUtils.unescapeHtml4(sWrongTopic.getAnswer()));
            sWrongTopic.setStem(StringEscapeUtils.unescapeHtml4(sWrongTopic.getStem()));
            return ObjectResult.build("1","成功",sWrongTopic);
        }
    }

    @RequestMapping(value = "/details")
    @ResponseBody
    public ModelAndView details(XMLBean xmlBean){
        SWrongTopic sWrongTopic = (SWrongTopic)wrongTopicService.wrongTopicDetails(xmlBean).getData();

        ModelAndView modelAndView = new ModelAndView("jiexi");
        modelAndView.addObject("id",xmlBean.getId());
        modelAndView.addObject("token",xmlBean.getToken());
        modelAndView.addObject("is_join",sWrongTopic.getIs_join());
        modelAndView.addObject("stem", StringEscapeUtils.unescapeHtml4(sWrongTopic.getStem()));
        modelAndView.addObject("answer",StringEscapeUtils.unescapeHtml4(sWrongTopic.getAnswer()));

        return modelAndView;
    }


    /**
     * 举一反三
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/learnByAnalogy",method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult learnByAnalogy(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.learnByAnalogy(xmlBean);
    }

    /**
     * 错题打印
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/printer")
    @ResponseBody
    public ObjectResult printer(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return wrongTopicService.wrongTopicPrinter(xmlBean);
    }

    /**
     * 学情分析
     * @param userId
     * @return
     */
    @RequestMapping(value = "/analysie")
    @ResponseBody
    public Map<String,Object> analysie(@RequestParam("userId") String userId,
                                       XMLBean xmlBean, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return wrongTopicService.analysie(userId,xmlBean.getId());
    }

    /**
     * 错题悬赏分享
     * @param xmlBean
     * @return
     */
    @RequestMapping("/share")
    @ResponseBody
    public ObjectResult share(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }

        SOfferReward sOfferReward = sOfferRewardMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        Map<String,Object> data = new HashMap<String,Object>();
        data.put("title","错题悬赏");
        data.put("content",sOfferReward.getContent());
        data.put("url", ConfigUtils.SYS_URL+"index/html/xuanshang?id="+xmlBean.getId());
        data.put("examine",ConfigUtils.EXAMINE);

        return ObjectResult.build("1","成功",data);
    }


    /**
     * 答案解析分享
     * @param xmlBean
     * @return
     */
    @RequestMapping("/answer")
    @ResponseBody
    public ObjectResult answer(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        SWrongTopic sWrongTopic = sWrongTopicMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        Map<String,Object> data = new HashMap<String,Object>();
        data.put("title","答案解析");
        data.put("content",StringUtil.getTextFromTHML(StringEscapeUtils.unescapeHtml4(sWrongTopic.getStem())));
        data.put("url", ConfigUtils.SYS_URL+"index/html/daanjiexi?id="+xmlBean.getId());
        data.put("examine",ConfigUtils.EXAMINE);

        return ObjectResult.build("1","成功",data);
    }
}
